Source for file GD.php
Documentation is available at GD.php
/***********************************************************************
** Title.........: GD Driver
** Author........: Xiang Wei ZHUO <wei@zhuo.org>
** Filename......: GD.php
** Last changed..: 30 Aug 2003
** Notes.........: Orginal is from PEAR
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Peter Bowyer <peter@mapledesign.co.uk> |
// | Alan Knowles <alan@akbkhome.com> |
// +----------------------------------------------------------------------+
// $img = new Image_Transform_GD();
// $img->load('magick.png');
// if($img->rotate($angle,array('autoresize'=>true,'color_mask'=>array(255,0,0)))){
// $img->addText(array('text'=>"Rotation $angle",'x'=>0,'y'=>100,'font'=>'/usr/share/fonts/default/TrueType/cogb____.ttf'));
// Image Transformation interface using the GD library
require_once "Transform.php";
* Holds the image file for manipulation
* Holds the original image file
* @return mixed true or or a PEAR error object on error
* @return mixed none or a PEAR error object on error
$this->uid =
md5($_SERVER['REMOTE_ADDR']);
$this->_get_image_details($image);
$functionName =
'ImageCreateFrom' .
$this->type;
* @param array options Array contains options
* 'text' The string to draw
* 'x' Horizontal position
* 'size' Size of the fonts in pixel
* 'resize_first' Tell if the image has to be resized
* before drawing the text
'text' =>
'This is Text',
'color' =>
array(255,0,0),
'resize_first' =>
false // Carry out the scaling of the image before annotation? Not used for GD
$this->colorhex2colorarray( $color );
include_once('Image/Transform/Driver/ColorsDefs.php');
$color = isset
($colornames[$color])?
$colornames[$color]:
false;
$c =
imagecolorresolve ($this->imageHandle, $color[0], $color[1], $color[2]);
if ('ttf' ==
substr($font, -
3)) {
ImageTTFText($this->imageHandle, $size, $angle, $x, $y, $c, $font, $text);
ImagePSText($this->imageHandle, $size, $angle, $x, $y, $c, $font, $text);
* Rotate image by the given angle
* Uses a fast rotation algorythm for custom angles
* or lines copy for multiple of 90 degrees
* @param int $angle Rotation angle
* @param array $options array( 'autoresize'=>true|false,
* 'color_mask'=>array(r,g,b), named color or #rrggbb
* @author Pierre-Alain Joye
* @return mixed none or a PEAR error object on error
function rotate($angle, $options=
null)
$white =
imagecolorallocate ($this->imageHandle, 255, 255, 255);
$color_mask =
array(255,255,0);
$this->colorhex2colorarray( $color_mask );
include_once('Image/Transform/Driver/ColorDefs.php');
$color = isset
($colornames[$color_mask])?
$colornames[$color_mask]:
false;
// Do not round it, too much lost of quality
$width =
$max_x =
$this->img_x;
$height =
$max_y =
$this->img_y;
$width2 = (int)
(abs(sin($t) *
$height +
cos($t) *
$width));
$height2 = (int)
(abs(cos($t) *
$height+
sin($t) *
$width));
$d_width =
abs($width -
$width2);
$d_height =
abs($height -
$height2);
$min_x2 = -
abs($x_offset);
$min_y2 = -
abs($y_offset);
$img2 =
ImageCreateTrueColor($width2,$height2);
$img2 =
ImageCreate($width2,$height2);
return false;/*PEAR::raiseError('Cannot create buffer for the rotataion.',
null, PEAR_ERROR_TRIGGER, E_USER_NOTICE);*/
imagepalettecopy($img2,$img);
$mask =
imagecolorresolve($img2,$color_mask[0],$color_mask[1],$color_mask[2]);
// use simple lines copy for axes angles
imagefill ($img2, 0, 0,$mask);
for ($y=
0; $y <
$max_y; $y++
) {
for ($x =
$min_x; $x <
$max_x; $x++
){
$c =
@imagecolorat ( $img, $x, $y);
imagesetpixel($img2,$x+
$x_offset,$y+
$y_offset,$c);
imagefill ($img2, 0, 0,$mask);
for ($x =
$min_x; $x <
$max_x; $x++
){
for ($y=
$min_y; $y <
$max_y; $y++
) {
$c =
imagecolorat ( $img, $x, $y);
imagesetpixel($img2,$max_y-
$y-
1,$x,$c);
imagefill ($img2, 0, 0,$mask);
for ($y=
0; $y <
$max_y; $y++
) {
for ($x =
$min_x; $x <
$max_x; $x++
){
$c =
@imagecolorat ( $img, $x, $y);
imagesetpixel($img2, $max_x2-
$x-
1, $max_y2-
$y-
1, $c);
imagefill ($img2, 0, 0,$mask);
for ($y=
0; $y <
$max_y; $y++
) {
for ($x =
$max_x; $x >=
$min_x; $x--
){
$c =
@imagecolorat ( $img, $x, $y);
imagesetpixel($img2,$y,$max_x-
$x-
1,$c);
// simple reverse rotation algo
for ($y =
$min_y2; $y <
$max_y2; $y++
){
$x2 =
round((($min_x2-
$x1) *
$cosT) +
(($y-
$y1) *
$sinT +
$x1),0);
$y2 =
round((($y-
$y1) *
$cosT -
($min_x2-
$x1) *
$sinT +
$y1),0);
for ($x =
$min_x2; $x <
$max_x2; $x++
){
// Check if we are out of original bounces, if we are
// use the default color mask
if ( $x2>=
0 &&
$x2<
$max_x &&
$y2>=
0 &&
$y2<
$max_y ){
$c =
imagecolorat ( $img, $x2, $y2);
imagesetpixel($img2,$x+
$x_offset,$y+
$y_offset,$c);
* For GD 2.01+ the new copyresampled function is used
* It uses a bicubic interpolation algorithm to get far
* @param $new_x int new width
* @param $new_y int new height
* @return true on success or pear error
if ($this->resized ===
true) {
return false; /*PEAR::raiseError('You have already resized the image without saving it. Your previous resizing will be overwritten', null, PEAR_ERROR_TRIGGER, E_USER_NOTICE);*/
$new_img =
ImageCreateTrueColor($new_x,$new_y);
$new_img =
ImageCreate($new_x,$new_y);
ImageCopyResampled($new_img, $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y);
ImageCopyResized($new_img, $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y);
* @param int $crop_x left column of the image
* @param int $crop_y top row of the image
* @param int $crop_width new cropped image width
* @param int $crop_height new cropped image height
function crop($new_x, $new_y, $new_width, $new_height)
$new_img =
ImageCreateTrueColor($new_width,$new_height);
$new_img =
ImageCreate($new_width,$new_height);
ImageCopyResampled($new_img, $this->imageHandle, 0, 0, $new_x, $new_y,$new_width,$new_height,$new_width,$new_height);
ImageCopyResized($new_img, $this->imageHandle, 0, 0, $new_x, $new_y, $new_width,$new_height,$new_width,$new_height);
* Flip the image horizontally or vertically
* @param boolean $horizontal true if horizontal flip, vertical otherwise
function flip($horizontal)
for ($j =
0; $j <
$height; $j++
) {
//echo " j:".$j." l:".$left." r:".$right."\n<br>";
* @param float $outputgamma
function gamma($outputgamma=
1.0) {
ImageGammaCorrect($this->imageHandle, 1.0, $outputgamma);
* @param $filename string the name of the file to write to
* @param $quality int output DPI, default is 85
* @param $types string define the output format, default
* is the current used format
function save($filename, $type =
'', $quality =
85)
$type =
$type==
''?
$this->type :
$type;
$functionName =
'image' .
$type;
$functionName($this->imageHandle, $filename, $quality);
* Display image without saving and lose changes
* @param string type (JPG,PNG...);
function display($type =
'', $quality =
75)
$functionName =
'Image' .
$this->type;
Documentation generated on Mon, 05 May 2008 16:19:59 +0400 by phpDocumentor 1.4.0